
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include "gpio.h"


#define GPIO_DEV "/dev/gpio_sg"

#define	TGPIO_IOC_MAGIC		'T'

#define TGPIO_IOC_GETVAL	_IOR(TGPIO_IOC_MAGIC, 1, uint8_t)
#define TGPIO_IOC_GETSTA	_IOR(TGPIO_IOC_MAGIC, 2, uint8_t)
#define TGPIO_IOC_SETVAL	_IOW(TGPIO_IOC_MAGIC, 1, uint8_t)
#define TGPIO_IOC_SETHIGH	_IOW(TGPIO_IOC_MAGIC, 2, uint8_t)
#define TGPIO_IOC_SETLOW	_IOW(TGPIO_IOC_MAGIC, 3, uint8_t)
#define TGPIO_IOC_TOGGLE	_IOW(TGPIO_IOC_MAGIC, 4, uint8_t)

#define GH	1
#define GL	0


char gpio_dev[128] = GPIO_DEV;
int status = 1;
int pin = 4;

GPIO_DO_STATE  g_do_state;
GPIO_DI_STATE  g_di_state;

enum tgpio_flagemu {
	lte_ctrl	= 0,
	lte_onoff	= 1,
	lte_rst		= 2,
    lte_led		= 3,
	pcie_en		= 4,
	usbuartrst	= 5,
	blerst		= 6,
	encryptrst	= 7,
	mculed1		= 8,
    mculed2		= 9,
    kx1_led		= 10,
    kx2_led		= 11,
    wan_gled	= 12,
    wan_rled	= 13,
    pcie_gled	= 14,
    pcie_rled	= 15,
    ble_led		= 16,
	yk1		    = 17,
	yk2		    = 18,
	gpiomax		= 19
};

typedef int QINT;

/* ------------------------------- */
/* GPIO interrupt */
//输入IO
enum intr_idxenu {
        lte_status      = 0,
        kx1             = 1,
        kx2             = 2,
        intrmax         = 3
};

int gpio_set(int gpio_idx, int req_code)
{
	QINT rtn = -1;
    QINT fdGpio = -1;
    
    fdGpio = open(gpio_dev, O_RDWR|O_NOCTTY|O_NDELAY);
    if (fdGpio < 0)
    {
        printf("Fail to open device '%s'!\n", gpio_dev);
        return rtn;
    }

    rtn = ioctl(fdGpio, req_code, &gpio_idx);

    close(fdGpio);
        
	return rtn;
}

int gpio_get(int gpio_idx)
{
	QINT rtn = -1;
    QINT fdGpio = -1;
    
    fdGpio = open(gpio_dev, O_RDWR|O_NOCTTY|O_NDELAY);
    if (fdGpio < 0)
    {
        printf("Fail to open device '%s'!\n", gpio_dev);
        return rtn;
    }

    rtn = ioctl(fdGpio, TGPIO_IOC_GETVAL, &gpio_idx);
    if(rtn != 0)
    {
        gpio_idx = -1;
    }

    close(fdGpio);
        
	return gpio_idx;
}


QINT gpio_intr_get(int intr_idx)
{
	QINT rtn = -1;
    QINT fdGpio = -1;
    
    fdGpio = open(gpio_dev, O_RDWR|O_NOCTTY|O_NDELAY);
    if (fdGpio < 0)
    {
        printf("Fail to open device '%s'!\n", gpio_dev);
        return rtn;
    }

    rtn = ioctl(fdGpio, TGPIO_IOC_GETSTA, &intr_idx);
    if(rtn != 0)
    {
        intr_idx = -1;
    }

    close(fdGpio);
        
	return intr_idx;
}


int gpio_clear(int gpio_idx)
{
	QINT rtn = -1;
    QINT fdGpio = -1;
    
    fdGpio = open(gpio_dev, O_RDWR|O_NOCTTY|O_NDELAY);
    if (fdGpio < 0)
    {
        printf("Fail to open device '%s'!\n", gpio_dev);
        return rtn;
    }

    rtn = ioctl(fdGpio, TGPIO_IOC_SETLOW, &gpio_idx);

    close(fdGpio);
        
	return rtn;
}

/**/
int DO1_turn_on(){
    if(0 > gpio_set(17, TGPIO_IOC_SETHIGH)){
        return -1;
    }
    g_do_state.GPIO_DO_17 = 1;
    return 0;
}
int DO1_turn_off(){
    if(0 > gpio_set(17, TGPIO_IOC_SETLOW)){
        return -1;
    }
    g_do_state.GPIO_DO_17 = 0;
    return 0;
}
int DO1_switch(uint8_t state){
    if(0 == state){
        return DO1_turn_off();
    } else {
        return DO1_turn_on();
    }
}
int DO2_turn_on(){
    if(0 > gpio_set(18, TGPIO_IOC_SETHIGH)){
        return -1;
    }
    g_do_state.GPIO_DO_18 = 1;
    return 0;
}
int DO2_turn_off(){
    if(0 > gpio_set(18, TGPIO_IOC_SETLOW)){
        return -1;
    }
    g_do_state.GPIO_DO_18 = 0;
    return 0;
}
int DO2_switch(uint8_t state){
    if(0 == state){
        return DO1_turn_off();
    } else {
        return DO1_turn_on();
    }
}
int DO1_get_state(){
    return gpio_get(17);
}

int DO2_get_state(){
    return gpio_get(18);
}

int DI1_get_state(){
    return gpio_intr_get(1);
}

int DI2_get_state(){
    return gpio_get(2);
}